package com.learn.hibernatevalidator.chapter02.containerelement.set;

import jakarta.validation.Constraint;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import jakarta.validation.Payload;

import java.lang.annotation.*;

/**
 * @author: liyf
 * @create: 2024-12-19 23:01
 **/
@Documented
@Constraint(validatedBy = ValidPart.ValidPartValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.PARAMETER, ElementType.TYPE_USE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ValidPart {

    String message() default "{com.learn.hibernatevalidator.message}";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};

    class ValidPartValidator implements ConstraintValidator<ValidPart, String> {

        @Override
        public void initialize(ValidPart constraintAnnotation) {
            ConstraintValidator.super.initialize(constraintAnnotation);
        }

        @Override
        public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
            return value != null && value.trim().length() > 0;
        }
    }
}
